package com.ts.utils.EGL

import android.opengl.EGL14
import android.opengl.EGL14.EGL_CONTEXT_CLIENT_VERSION
import android.opengl.EGL14.EGL_PBUFFER_BIT
import android.opengl.EGL14.EGL_RED_SIZE
import android.opengl.EGLConfig
import android.opengl.EGLDisplay
import android.opengl.EGLExt.EGL_OPENGL_ES3_BIT_KHR
import android.util.Log
import javax.microedition.khronos.egl.EGL10.EGL_ALPHA_SIZE
import javax.microedition.khronos.egl.EGL10.EGL_BLUE_SIZE
import javax.microedition.khronos.egl.EGL10.EGL_DEPTH_SIZE
import javax.microedition.khronos.egl.EGL10.EGL_GREEN_SIZE
import javax.microedition.khronos.egl.EGL10.EGL_NONE
import javax.microedition.khronos.egl.EGL10.EGL_STENCIL_SIZE
import javax.microedition.khronos.egl.EGL10.EGL_SURFACE_TYPE

class EGLutil {

    private  val TAG = "EGLutil"
    var mEGLDisplay: EGLDisplay?=null

    var major=IntArray(1)
    val minor=IntArray(1)

    var config =arrayOfNulls<EGLConfig>(1)



    var confAttr= intArrayOf(
        EGL14.EGL_RENDERABLE_TYPE,EGL_OPENGL_ES3_BIT_KHR,
        EGL_SURFACE_TYPE,EGL_PBUFFER_BIT,
        EGL_RED_SIZE,8,
        EGL_GREEN_SIZE,8,
        EGL_BLUE_SIZE,8,
        EGL_ALPHA_SIZE,8,
        EGL_DEPTH_SIZE, 16,
        EGL_STENCIL_SIZE,8,
        EGL_NONE
    )
    val numConfigs = IntArray(1)
    val ctxAttr= intArrayOf(
        EGL_CONTEXT_CLIENT_VERSION, 2,
        EGL_NONE
    )
    fun initEgl(){
        mEGLDisplay= EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY)

       if (! EGL14.eglInitialize(mEGLDisplay,major,0,minor,0)){
           Log.e(TAG,"eglInitialize fail")
       }

        val ischooseConfig= EGL14.eglChooseConfig(mEGLDisplay, confAttr, 0,config,0,config.size,numConfigs,0)
        if (!ischooseConfig){
            Log.e(TAG,"eglChooseConfig fail")
        }
        var mEGLContext=EGL14.eglCreateContext(mEGLDisplay,config[0],EGL14.EGL_NO_CONTEXT,ctxAttr,0)

        var mEGLSurface= EGL14.eglCreatePbufferSurface(mEGLDisplay,config[0],confAttr,0)

        EGL14.eglMakeCurrent(mEGLDisplay,mEGLSurface,mEGLSurface,mEGLContext)
    }
}